package cn.tedu.hospitalgroupproject.server.content.dao.persist.repository.impl;

import cn.tedu.hospitalgroupproject.server.common.pojo.vo.PageData;
import cn.tedu.hospitalgroupproject.server.common.util.PageInfoToPageDataConverter;
import cn.tedu.hospitalgroupproject.server.content.dao.persist.mapper.DepartmentMapper;
import cn.tedu.hospitalgroupproject.server.content.dao.persist.repository.IDepartmentRepository;
import cn.tedu.hospitalgroupproject.server.content.pojo.entity.Department;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DepartmentListVO;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DepartmentStandardVO;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DoctorTitleAppLimitListVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class DepartmentRepositoryImpl implements IDepartmentRepository {

    @Autowired
    private DepartmentMapper departmentMapper;


    public DepartmentRepositoryImpl() {
        log.info("创建数据库对象:DepartmentRepositoryImpl");
    }

    @Override
    public int insert(Department department) {
        log.debug("开始向[科室表]中插入数据:{}",department);
        return departmentMapper.insert(department);
    }

    @Override
    public int deleteById(Long id) {
        log.debug("开始执行[删除指定科室ID数据]:{}",id);
        return departmentMapper.deleteById(id);
    }

    @Override
    public int updateById(Department department) {
        log.debug("开始[执行根据ID更新指定的科室信息]的数据访问,department:{}",department);
        return departmentMapper.updateById(department);
    }

    @Override
    public int countByNameAndNotId(Long id,String name) {
        log.debug("开始[执行根据名字统计相同科室信息条数]的数据访问,id:{},name:{}",id,name);
        QueryWrapper<Department> wrapper=new QueryWrapper<>();
        wrapper.eq("name",name).ne("id",id);
        return departmentMapper.selectCount(wrapper);
    }

    @Override
    public DepartmentStandardVO getStandardById(Long id) {
        log.debug("开始[执行根据ID查询指定科室信息]的数据访问,id:{}",id);
        return departmentMapper.getStandardById(id);
    }

    @Override
    public PageData<DepartmentListVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询科室列表】，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<DepartmentListVO> list = departmentMapper.listDepartment();
        PageInfo<DepartmentListVO> pageInfo = new PageInfo<>(list);
        PageData<DepartmentListVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
